/*
 * USER PREFERENCES
 */

MyDesktop.Settings = Ext.extend(Ext.app.Module, {
	id: 'user-settings',
    init : function(){
        this.launcher = {
            text: _('User Preferences'),
            iconCls:'settings',
            handler : this.createWindow,
            scope: this,
            windowId: 'user-settings-win'
        }
    },
	
    createWindow : function(src){
        var desktop = this.app.getDesktop();
        var winname= src.windowId;
        var win = desktop.getWindow(winname);
        if(!win){
			var statusBar;
			
        	var prefPanel = new Ext.ux.JsonPanel({autoLoad: getMainUrl('action=user_preferences'),msg: _('Loading...')}); 

		    var prefForm = new Ext.form.FormPanel({
		    	id: 'userpreferences-form',
		        url: getMainUrl('action=user_preferences&op=save'),
		        items: prefPanel,
		        waitMsgTarget: true,
		        frame: true,
	        	height:300,
				tbar: [{
					text: _('Save'),
					tooltip: _('Save'),
					iconCls: 'save',
					handler: function() {
		                	prefForm.initFields();
                    		if (prefForm.getForm().isValid()) {
	                    		prefForm.getForm().submit({ 
		                        	method:'POST', 
		                        	waitTitle: _('Saving...'), 
		                        	waitMsg: _('Saving...'),
		
			                        success:function(form, action){
			                        	obj = Ext.util.JSON.decode(action.response.responseText);
			                        	if (obj.ret=="reload") {
									        Ext.Msg.confirm(_('Confirm'), _('Some change requires to reload the page. Do you want to do it now?'), function(btn) {
									        																							 			if (btn=="yes") {
									        																							 				window.location.reload();	
									        																							 			} 
									        																							 		} 
									        																							 	);
			                        	}
			                        	userLocale = Ext.getCmp("userLocaleField").value;
			                        	win.close();
			                        }                     	
	                    		});
                    			}
                    		}
				},{
					text: _('Preferences'),
					tooltip: _('Preferences'),
					iconCls: 'settings',
					handler: function() {
								var m = new MyDesktop.Preferences();
								m.app = MyDesktop;
								m.createWindow();
                    		}
				}]		        	
			});        	

            win = desktop.createWindow({
                id: winname,
                title:src.text,
                width:500,
                height:300,
                //height:330,
                //height:430,
                resizable: false,
                maximizable: false,
                iconCls: 'settings',
                items: prefForm
            });
            
        }
        win.show();
        
    }
});